package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserServiceImpl userService;

    //用户是要关注还是取消关注博主
    @Override
    public Result Follow(Long id, Boolean isFollow) {
        //获取当前用户id
        Long userId = UserHolder.getUser().getId();
        //redis中的关注set集合前缀
        String key="follow:";
        //判断当前是关注还是取消
        if (isFollow) {
            //创建follow对象
            Follow follow=new Follow();
            follow.setFollowUserId(id);
            follow.setUserId(userId);
             boolean save = save(follow);
             if(save){
                 //数据库保存成功,接下来存储在reids中的set中
                    stringRedisTemplate.opsForSet().add(key+userId,id.toString());
             }
        }
        else{
            boolean remove = remove(new QueryWrapper<Follow>().eq("user_id", userId).eq("follow_user_id", id));
            if(remove)
            {
                //取消关注成功,从redis中删除
                stringRedisTemplate.opsForSet().remove(key+userId,id.toString());
            }
        }
        return Result.ok();
    }

    //用户是否关注了博主
    @Override
    public Result isFollow(Long id) {
        //通过查数据库看能否查出来即可
        Long userId = UserHolder.getUser().getId();
        Integer count = query().eq("user_id", userId).eq("follow_user_id", id).count();
        return Result.ok(count>0);
    }

    @Override
    public Result isCommon(Long id) {
        //获取当前用户的id
        Long userId = UserHolder.getUser().getId();
        //redis中的两个set键值
        String key1= "follow:"+userId;
        String key2="follow:"+id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if(intersect==null||intersect.isEmpty())
        {
            return Result.ok(Collections.emptyList());
        }
        //解析id集合
        List<Long> collect = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        //去数据库查
        List<UserDTO> userDTOList = userService.listByIds(collect).stream().map(user ->
                BeanUtil.copyProperties(user, UserDTO.class)).collect(Collectors.toList());
        return Result.ok(userDTOList);
    }
}
